<?php

namespace app\admin\logic;

use nx\Data;
use think\Cache;
use app\admin\model\Source as Source_model;

class Source
{
    public $Source_model = '';

    /**获取模型
     * @return Source_model|string
     */
    public function getModel(){
        if (empty($this->Source_model)){
            $this->Source_model = new Source_model();
        }
        return $this->Source_model;
    }

    /**获取所有的来源来源（供获取器使用）
     * @return array|false|mixed|\PDOStatement|string|\think\Collection
     */
    public static function SourcesToText(){
        $Sources = self::getAllSources();
        $Sources = collect($Sources)->column('title','id');
        return $Sources;
    }

    /**获取所有的来源来源（供获取器使用）
     * @return array|false|mixed|\PDOStatement|string|\think\Collection
     */
    public static function SourcesToInt(){
        $Sources = self::getAllSources();
        $Sources = collect($Sources)->column('id','title');
        return $Sources;
    }
    
    /**将所有来源进行树形排列
     * @return array|false|mixed|\PDOStatement|string|\think\Collection
     */
    public function getAllSourceToTree(){
        $Sources = $this->getAllSources();
        $Sources = Data::tree($Sources,'title','id','pid');
        return $Sources;
    }

    /**获得所有的来源来源（从缓存中）
     * @return false|mixed|\PDOStatement|string|\think\Collection   返回数组
     */
    public function getAllSources(){
        $Sources=Cache::get('allLaiyuans');
        if (!empty($Sources)){
            return $Sources;
        }else{
            $Sources = Source_model::all();
            $Sources= collection($Sources)->toArray();
            Cache::set('allSources',$Sources);
            return $Sources;
        }
    }

    /**获取所有的来源
     * @return mixed
     */
    public function getAllCompanys(){
        $Laiyuans = $this->getAllSources();
        $companys = collect($Laiyuans)->where('pid',0)->all();
        return $companys;
    }

    /**
     * 获取某来源的所有下属来源
     * @param $Laiyuan_id
     * @return array|mixed
     */
    public function getNextLaiyuans($Laiyuan_id){
        $data = $this->getAllSources();
        $Laiyuans = Data::channelList($data,$Laiyuan_id,'— ','id','pid');
        return $Laiyuans;
    }


    public function addLaiyuan($data){
        if(empty($data)){
            return ['result'=>false,'msg'=>'数据丢失'];
        }
        if (!empty($data['code'])){
            $data['code'] = strtoupper(trim($data['code']));
        }
        $result = $this->getModel()->validate(true)->save($data);
        if(false === $result){
            return ['result'=>false,'msg'=>$this->getModel()->getError()];
        }else{
            Cache::rm('allLaiyuans');
            return ['result'=>true,'msg'=>'来源添加成功'];
        }
    }

    public function editLaiyuan($data){
        if(empty($data['id'])){
            return ['result'=>false,'msg'=>'ID丢失'];
        }
        isset($data['status'])?:$data['status']=1;
        $result = $this->getModel()->allowField(true)->validate(true)->isUpdate(true)->save($data);
        if(false === $result){
            return ['result'=>false,'msg'=>$this->getModel()->getError()];
        }else{
            Cache::rm('allLaiyuans');
            return ['result'=>true,'msg'=>'来源修改成功'];
        }
    }

    public function delLaiyuan($id){
        if ( empty($id) ) {
            return ['result'=>false,'msg'=>'请选择要操作的数据'];
        }
        if(Source_model::get($id)){
            if(Source_model::destroy($id)){
                Cache::rm('allLaiyuans');
                return ['result'=>true,'msg'=>'删除成功'];
            } else {
                return ['result'=>false,'msg'=>'删除失败'];
            }
        }else{
            return ['result'=>false,'msg'=>'要删除的数据不存在'];
        }
    }

  





}
